package com.chainyoung.component_tmperson.ui.mvp.model;

import com.chainyoung.common.appcommon.hdwallet.EtherscanService;
import com.chainyoung.common.appcommon.hdwallet.ResponseParser;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.integration.IRepositoryManager;
import com.chainyoung.common.mvp.BaseModel;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletSendTransactionContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletLocalContract;


import org.json.JSONObject;

import java.math.BigInteger;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;

@ActivityScope
public class HdWalletSendTransactionModel extends BaseModel implements HdWalletSendTransactionContract.Model {

    @Inject
    HdWalletSendTransactionModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);

    }


    @Override
    public Flowable<String> getGasPrice() {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getGasPrice()
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }

    @Override
    public Flowable<String> sendTransaction(String hex) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .sendTransaction(hex)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {

                        JSONObject jsonObject = new JSONObject(body.string());
                        if (jsonObject.has("result")) {
                            return jsonObject.getString("result");
                        } else if (jsonObject.has("error")) {
                            JSONObject errorObject = new JSONObject(jsonObject.getString("error"));
                            throw new ApiException(errorObject.getString("code"),errorObject.getString("message"));
                        }
                        return null;
                        // return new JSONObject(body.string()).getString("result");
                    }
                });
    }

    @Override
    public Flowable<String> getNonce(String addr) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getTransactionCount(addr)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }

    @Override
    public Flowable<String> getEthPrice() {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getEthPrice()
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("result");
                    }
                });
    }


    @Override
    public Flowable<String> getUsdCnyRate(String from, String to) {
        return mRepositoryManager.obtainRetrofitService(EtherscanService.class)
                .getUsdCnyRate(from, to)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(@NonNull ResponseBody body) throws Exception {
                        return new JSONObject(body.string()).getString("data");
                    }
                });
    }


}
